package com.itheima.controller;


//import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.aspect.annotation.OperLog;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        //MD5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        if(emp == null){
            return R.error("登陆失败");
        }
        //BCrypt加密
//        if(!BCrypt.checkpw(password,emp.getPassword())){
//            return R.error("登陆失败");
//        }
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }
        if(emp.getStatus() == 0){
            return R.error("账号禁用");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    //退出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //分页查询
//    @GetMapping("/page")
//    public R<Page> pageEmp(int page,int pageSize,String name){
//        //封装Page对象
//        Page page1 = new Page(page,pageSize);
//        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        //模糊查询
//        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
//        lqw.orderByAsc(Employee::getUpdateTime);
//        employeeService.page(page1,lqw);
//        return R.success(page1);
//    }


    //参数封装为map进行分页查询
    //@Transactional
    @OperLog(value = "分页查询员工信息",username = "admin")
    @GetMapping("/page")
    public R<Page> pageEmp(@RequestParam Map map){
        Page page1 = new Page(Long.parseLong((String) map.get("page")),Long.parseLong((String)map.get("pageSize")));
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(StringUtils.isNotEmpty((String) map.get("name")),Employee::getName,map.get("name"));
        lqw.orderByAsc(Employee::getUpdateTime);
        employeeService.page(page1,lqw);
        return R.success(page1);
    }

    //@Transactional
    @OperLog(value = "新增员工",username = "admin")
    //新增员工
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        //设置密码并用MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //修改禁用启用状态
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //获取员工id
        Long empId = (Long) request.getSession().getAttribute("employee");
        //修改时间改为当前时间
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    //根据id查询员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到该员工信息");
    }


}
